package com.admin.kotlintest.fragment

import android.os.Handler
import android.widget.Button
import cn.bmob.v3.BmobPushManager
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.PushListener
import cn.bmob.v3.listener.UpdateListener
import com.admin.kotlintest.R
import com.admin.kotlintest.adapter.AppiontmentRecordAdapter
import com.admin.kotlintest.base.BaseFragment
import com.admin.kotlintest.bean.Appointment
import com.admin.kotlintest.bean.Installation
import com.admin.kotlintest.bean.MyUser
import com.admin.kotlintest.config.FixedValue
import com.admin.kotlintest.config.MyApplication
import com.admin.kotlintest.utils.SystemUtils
import com.socks.library.KLog
import kotlinx.android.synthetic.main.fragment_appiontment_record.*
import org.jetbrains.anko.support.v4.toast
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by 吴昶 on 2018/4/20.
 */
class AppiontmentRecordFragment:BaseFragment(),AppiontmentRecordAdapter.AcceptRefuseListener{

    var showDataType=0
    var recordAdapter:AppiontmentRecordAdapter?=null
    var appointmentList=ArrayList<Appointment>()
    var myUser=MyUser()
    var typeName:String=""

    var handler=Handler(Handler.Callback {
        when(it.what){
            9->{
                recordAdapter!!.upData(appointmentList)
                KLog.d(appointmentList.size.toString())
            }
        }
        false
    })

    override fun bindLayout(): Int {
        return R.layout.fragment_appiontment_record
    }

    override fun initData() {
        myUser=BmobUser.getCurrentUser(MyUser::class.java)
        typeName = MyApplication.getSharedPreferencesString(FixedValue.TYPE_NAME,"").toString()
        recordAdapter= AppiontmentRecordAdapter(context,R.layout.layout_appiontment_record_item,appointmentList,this@AppiontmentRecordFragment)
        lv_record_list.adapter=recordAdapter
    }

    override fun fetchData() {
        updata()
    }

    fun updata(){
        appointmentList.clear()
        var query=BmobQuery<Appointment>()
        when(typeName){
            "司机"->{
                query.addWhereEqualTo("driverId",myUser.objectId)
            }
            else->{
                query.addWhereEqualTo("userId",myUser.objectId)
            }
        }
        query.addWhereEqualTo("appointState",showDataType)
        query.findObjects(object:FindListener<Appointment>(){
            override fun done(p0: MutableList<Appointment>?, p1: BmobException?) {
                p1?:let {
                    appointmentList.addAll(p0!!)
                    recordAdapter!!.upData(appointmentList)
                }

                p1?.let {
                    KLog.d(p1.message)
                }
            }
        })
    }

    override fun AcceptListener(item: Appointment, accept: Button, refuse: Button) {
        KLog.d(item.toString())
        var updata=item
        updata.driverAccept=true
        updata.appointState=1
        updata.update(item.objectId,object:UpdateListener(){
            override fun done(p0: BmobException?) {
                p0?:let {
                    toast("签收成功")
                    var msg=SystemUtils.installationMsgFormat("司机接受用车申请","您的用车申请 # ${item.appointRemarks} # 司机已接受")
                    var user=MyUser()
                    user.objectId=item.userId
                    var manager=BmobPushManager<Installation>()
                    var query=BmobQuery<Installation>()
                    query.addWhereEqualTo("myUser",user)
                    manager.query=query
                    manager.pushMessage(msg,object:PushListener(){
                        override fun done(p0: BmobException?) {
                            p0?:let {
                                updata()
                            }
                            p0?.let { KLog.d(p0.message) }
                        }
                    })
                }
            }
        })
    }

    override fun RefuseListener(item: Appointment, accept: Button, refuse: Button) {

    }


}